wx.$utils = {
	//获取支付信息
	orderPay: function(obj,okFn, errFn){
		wx.$http({
			load: true,
			site: 'phpSite',
			url: '/user/pay',
			data: {
				order_no: obj.orderNo,
				order_type: obj.orderType,//1=跑腿，2=家政，3=互助
				pay_type: obj.payType || 1,//1=订单支付，2=小费支付，3=打赏支付，4=家政支付，5=互助支付，6=差价支付
				price: obj.price || 0
			}
		}).then((res) => {
			let data = res.data;
			if(data.code == 0){
				wx.showToast({
					title: '发布成功！',
					success: function() {
						setTimeout(() => {
							okFn();
						}, 1500)
					}
				})
				return;
			}
			wx.requestPayment({
				timeStamp: String(data.timeStamp),
				nonceStr: data.nonceStr,
				package: data.package,
				signType: 'MD5',
				paySign: data.paySign,
				success(res) {
					wx.showToast({
						title: '支付成功',
						success: function() {
							setTimeout(() => {
								okFn();
							}, 1500)
						}
					})
				},
				fail(err) {
					console.log(err,'支付失败原因')
					wx.showToast({
						title: '支付失败',
						icon: 'none',
						success: function() {
							setTimeout(() => {
								errFn();
							}, 1500)
						}
					})
				}
			})
		})
	},

	
	//计算两个坐标的距离
	getDistance(start, end) {
		const earthRadiusKm = 6371;
		const startLatRadians = degreesToRadians(start.lat);
		const endLatRadians = degreesToRadians(end.lat);
		const latDiffRadians = degreesToRadians(end.lat - start.lat);
		const lonDiffRadians = degreesToRadians(end.lon - start.lon);
		const a = Math.sin(latDiffRadians / 2) * Math.sin(latDiffRadians / 2) +
			Math.cos(startLatRadians) * Math.cos(endLatRadians) *
			Math.sin(lonDiffRadians / 2) * Math.sin(lonDiffRadians / 2);
		const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		const distance = earthRadiusKm * c;
		const distanceMeters = distance * 1000;
		return distanceMeters;
	},
	
	//返回
	goBack: function () {
		if (getCurrentPages().length > 1) {
			wx.navigateBack();
		} else {
			wx.reLaunch({
				url: '/pages/index/index',
			});
		}
    },
    
    //上传
    uploadImg: function(obj,fn,fn2) {
        wx.chooseMedia({
            count: obj.count,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            camera: 'back',
            success(res) {
                if (res.errMsg == 'chooseMedia:ok') {
                    wx.showLoading({
                        title: '上传...',
                    })
                    res.tempFiles.forEach(item => {
                        wx.uploadFile({
                            url: wx.$PUBLIC['phpSite'] + '/user/upload-file',
                            filePath: item.tempFilePath,
                            name: 'file',
                            formData: {
                                'user': obj.formData || ''
                            },
                            success(res2) {
                                const data = JSON.parse(res2.data);
                                if (data.code == 1) {
                                    let url = data.data.url;
                                    fn(url);
                                    if(fn2){
                                        fn2(res.tempFiles)
                                    }
                                } else {
                                    wx.showToast({
                                        title: '上传失败，请稍后再试',
                                        icon: 'none'
                                    })
                                }
                            },
                            fail(err) {
                                wx.showToast({
                                    title: '上传失败，请稍后再试',
                                    icon: 'none'
                                })
                            },
                            complete() {
                                wx.hideLoading();
                            }
                        })
                    })
                } else {
                    wx.showToast({
                        title: '上传失败，请稍后再试',
                        icon: 'none'
                    })
                }

            },
            fail: function (err) {
                console.log(err, '上传失败')
            }
        })
    }
};

function degreesToRadians(degrees) {
	return degrees * Math.PI / 180;
}
module.exports = wx.$utils;
